package com.zhhs.galaxyott.http.api;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.zhhs.galaxyott.Constant;
import com.zhhs.galaxyott.beans.PlayUrlBean;
import com.zhhs.galaxyott.httpVolley.JsonRequestManager;
import com.zhhs.galaxyott.httpVolley.base.ResponseListener;
import com.zhhs.galaxyott.httpVolley.parser.CategoryListParser;
import com.zhhs.galaxyott.httpVolley.parser.CategoryMoviesParser;
import com.zhhs.galaxyott.httpVolley.parser.CollectionParser;
import com.zhhs.galaxyott.httpVolley.parser.EmptyParser;
import com.zhhs.galaxyott.httpVolley.parser.HomemovieListParser;
import com.zhhs.galaxyott.httpVolley.parser.HotMoviesParser;
import com.zhhs.galaxyott.httpVolley.parser.IsVipParser;
import com.zhhs.galaxyott.httpVolley.parser.MoreSpecialContentParser;
import com.zhhs.galaxyott.httpVolley.parser.MoreSpecialTypeParser;
import com.zhhs.galaxyott.httpVolley.parser.MovieDetailParser;
import com.zhhs.galaxyott.httpVolley.parser.MovieDetailRecParser;
import com.zhhs.galaxyott.httpVolley.parser.PlayUrlParser;
import com.zhhs.galaxyott.httpVolley.parser.SingleSpecialParser;
import com.zhhs.galaxyott.httpVolley.parser.SpecialExpandParser;
import com.zhhs.galaxyott.httpVolley.parser.UniversalParser;
import com.zhhs.galaxyott.httpVolley.parser.VipDataParser;
import com.zhhs.galaxyott.httpVolley.parser.WatchHistoyParser;
import com.zhhs.galaxyott.httpVolley.parser.getPayPlatformURLParser;
import com.zhhs.galaxyott.httpVolley.response.CategoryListResponse;
import com.zhhs.galaxyott.httpVolley.response.CategoryMoviesResponse;
import com.zhhs.galaxyott.httpVolley.response.CollectionResponse;
import com.zhhs.galaxyott.httpVolley.response.EmptyResponse;
import com.zhhs.galaxyott.httpVolley.response.HomemovieListResponse;
import com.zhhs.galaxyott.httpVolley.response.HotMoviesResponse;
import com.zhhs.galaxyott.httpVolley.response.IsVipResponse;
import com.zhhs.galaxyott.httpVolley.response.MoreSpeciaContentlResponse;
import com.zhhs.galaxyott.httpVolley.response.MoreSpecialTypeResponse;
import com.zhhs.galaxyott.httpVolley.response.MovieDetailRecommendResponse;
import com.zhhs.galaxyott.httpVolley.response.MovieDetailResponse;
import com.zhhs.galaxyott.httpVolley.response.SingleSpeciaResponse;
import com.zhhs.galaxyott.httpVolley.response.SpecialExpandResponse;
import com.zhhs.galaxyott.httpVolley.response.UniversalResponse;
import com.zhhs.galaxyott.httpVolley.response.VipDataResponse;
import com.zhhs.galaxyott.httpVolley.response.WatchHistoryResponse;
import com.zhhs.galaxyott.httpVolley.response.getPayPlatformURLResponse;

public class RequestMaker {

    /**
     * 获取首页电影列表
     * 
     */
    public static void getHomeMovieList(Activity context, int page,
            ResponseListener<HomemovieListResponse> responseListener) {
        JSONObject json = new JSONObject();
        try {
            json.put("page", page);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/ad/list", json,
                new HomemovieListParser(), responseListener);
    }

    // /**
    // * 获取首页滚动信息
    // *
    // * @param context
    // * @param responseListener
    // */
    // public static void getRollingNews(Activity context,
    // ResponseListener<HomeRollingNewsResponse> responseListener) {
    // JSONObject json = new JSONObject();
    // JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL +
    // "/REST/page/rollingnews/list", json,
    // new HomeRollingNewsParser(), responseListener);
    // }
    //
    // /**
    // * 获取首页广告活动推荐
    // *
    // * @param context
    // * @param responseListener
    // */
    // public static void getHomeRecommand(Activity context,
    // ResponseListener<HomeRecommendResponse> responseListener) {
    // JSONObject json = new JSONObject();
    // JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL +
    // "/REST/page/recommend/list", json,
    // new HomeRecommendParser(), responseListener);
    // }

    /**
     * 获取专题扩展页数据
     * 
     * @param context
     * @param responseListener
     */
    public static void getSpecialExpand(Activity context, ResponseListener<SpecialExpandResponse> responseListener) {
        JSONObject json = new JSONObject();
        JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/special/extbooth/list", json,
                new SpecialExpandParser(), responseListener);
    }

    /**
     * 获取电影分页列表
     * 
     * @param context
     * @param responseListener
     */
    public static void getCategoryList(Activity context, ResponseListener<CategoryListResponse> responseListener) {
        JSONObject json = new JSONObject();
        JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/categoryDvbott/list", json,
                new CategoryListParser(), responseListener);
    }

    /**
     * 获取分页下电影列表
     * 
     * @param context
     * @param responseListener
     */
    public static void getCategoryMovie(Activity context, int categoryid, int pageNo, int pageRow,
            ResponseListener<CategoryMoviesResponse> responseListener) {
        JSONObject json = new JSONObject();

        try {
            json.put("categoryid", categoryid);
            json.put("pageNo", pageNo);
            json.put("pageRow", pageRow);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/categoryDvbott/movie", json,
                new CategoryMoviesParser(), responseListener);
    }

    /**
     * 获取多专题列表
     * 
     */
    public static void getMoreSpecialType(Context context, ResponseListener<MoreSpecialTypeResponse> responseListener) {
        JSONObject json = new JSONObject();
        JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/act/listType", json,
                new MoreSpecialTypeParser(), responseListener);
    }

    /**
     * 获取多专题列表
     * 
     */
    public static void getMoreSpecialContent(Context context, int typeId,
            ResponseListener<MoreSpeciaContentlResponse> responseListener) {
        JSONObject json = new JSONObject();
        try {
            json.put("typeId", typeId);

            JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/act/listContent", json,
                    new MoreSpecialContentParser(), responseListener);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取单专题详情
     * 
     */
    public static void getSingleSpecial(Activity context, int specialId,
            ResponseListener<SingleSpeciaResponse> responseListener) {
        JSONObject json = new JSONObject();
        try {
            json.put("specialId", specialId);

            Log.i("DEBUG_DT", "THIS IS getSingleSpecial--?" + specialId);

            JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/special/detail", json,
                    new SingleSpecialParser(), responseListener);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取热门电影列表
     * 
     */
    public static void getHotMovie(Activity context, int specialId, int pageNo, int pageRow,
            ResponseListener<HotMoviesResponse> responseListener) {
        JSONObject json = new JSONObject();

        try {
            json.put("specialId", specialId);
            json.put("pageNo", pageNo);
            json.put("pageRow", pageRow);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/special/listSpecialMovie", json,
                new HotMoviesParser(), responseListener);
    }

    /**
     * 获取电影详情
     * 
     */
    public static void getMovieDetail(Activity context, int pid, String smartcardid,
            ResponseListener<MovieDetailResponse> responseListener) {
        JSONObject json = new JSONObject();
        try {
            json.put("pid", pid);
            json.put("smartcardid", smartcardid);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/movie/detailDvbott", json,
                new MovieDetailParser(), responseListener);
    }

    /**
     * 获取电影详情推荐列表
     * 
     */
    public static void getMovieDetailRecommend(Activity context, int pageNo, int pageRow,
            ResponseListener<MovieDetailRecommendResponse> responseListener) {
        JSONObject json = new JSONObject();

        try {
            json.put("pageNo", pageNo);
            json.put("pageRow", pageRow);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/movies/listRecommend", json,
                new MovieDetailRecParser(), responseListener);
    }

    /************************** 用户相关接口 **********************************/

    /**
     * 添加收藏接口
     * 
     */
    public static void addCollected(Activity context, int pid, String smartcardid,
            ResponseListener<UniversalResponse> responseListener) {
        JSONObject json = new JSONObject();
        try {
            json.put("pid", pid);
            json.put("smartcardid", smartcardid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/favoriteDvbott/add", json,
                new UniversalParser(), responseListener);
    }

    /**
     * 取消收藏接口
     */
    public static void delCollected(Activity context, int pid, String smartcardid,
            ResponseListener<UniversalResponse> responseListener) {
        JSONObject json = new JSONObject();
        try {
            json.put("pid", pid);
            json.put("smartcardid", smartcardid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/favoriteDvbott/delete", json,
                new UniversalParser(), responseListener);
    }

    /**
     * 获取收藏记录 "smartcardid":"000000", "pageNo":0, "pageRow":3
     * 
     */
    public static void getCollectedMovies(Activity context, String smartcardid, int pageNo, int pageRow,
            ResponseListener<CollectionResponse> responseListener) {
        JSONObject json = new JSONObject();
        try {
            json.put("smartcardid", smartcardid);
            json.put("pageNo", pageNo);
            json.put("pageRow", pageRow);

            JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/favoriteDvbott/list", json,
                    new CollectionParser(), responseListener);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取用户购买记录
     * 
     */
    public static void getPurchaseRecord(Activity context, String smartcardid, String pageNo, String pageRow,
            ResponseListener<UniversalResponse> responseListener) {
        JSONObject json = new JSONObject();
        try {
            json.put("smartcardid", smartcardid);
            json.put("pageNo", pageNo);
            json.put("pageRow", pageRow);

            JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/user/order", json,
                    new UniversalParser(), responseListener);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存用户观看记录
     * 
     */
    public static void addWatchRecord(Activity context, int pid, String smartcardid, String watchtime,
            ResponseListener<UniversalResponse> responseListener) {
        JSONObject json = new JSONObject();
        try {
            json.put("pid", pid);
            json.put("smartcardid", smartcardid);
            json.put("watchtime", watchtime);

            JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/user/watchDvbott/record", json,
                    new UniversalParser(), responseListener);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取用户观看记录
     * 
     */
    public static void getWatchRecord(Activity context, String smartcardid, int pageNo, int pageRow,
            ResponseListener<WatchHistoryResponse> responseListener) {
        JSONObject json = new JSONObject();
        try {
            json.put("smartcardid", smartcardid);
            json.put("pageNo", pageNo);
            json.put("pageRow", pageRow);

            JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/user/watchDvbott/history", json,
                    new WatchHistoyParser(), responseListener);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 拼接统一支付地址
     * 
     */
    public static void getPayPlatformURL(String targetId, String cardNo, Double price, String returnUrl,
            String homeUrl, ResponseListener<getPayPlatformURLResponse> responseListener) {
        try {
            JSONObject json = new JSONObject();
            json.put("targetId", targetId);
            json.put("cardNo", cardNo);
            json.put("payAmount", price);
            json.put("returnUrl", returnUrl);
            json.put("homeUrl", homeUrl);
            System.out.println("------json" + json.toString());
            JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/order/getPayUrl", json,
                    new getPayPlatformURLParser(), responseListener);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 是否是vip
     * 
     */
    public static void getIsVip(String caId, ResponseListener<IsVipResponse> responseListener) {
        try {
            JSONObject json = new JSONObject();
            json.put("caId", caId);
            System.out.println("------json" + json.toString());
            JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/order/isVIP", json,
                    new IsVipParser(), responseListener);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 影片是否已经订购
     * 
     */
    // public static void getIsOrder(String caId, int movieId,
    // ResponseListener<IsOrderResponse> responseListener) {
    // try {
    // JSONObject json = new JSONObject();
    // json.put("caId", caId);
    // json.put("movieId", movieId);
    // System.out.println("------json" + json.toString());
    // JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL +
    // "/REST/movie/isAuth", json,
    // new IsOrderParser(), responseListener);
    // } catch (JSONException e) {
    // e.printStackTrace();
    // }
    // }

    /**
     * 获取套餐信息.
     * 
     */
    public static void getVipData(ResponseListener<VipDataResponse> responseListener) {
        JSONObject json = new JSONObject();
        JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/epgPackage/list", json,
                new VipDataParser(), responseListener);
    }

    /**
     * 生成订单接口
     * 
     */
    public static void addOrder(String smartcardid, String deviceId, int orderType, Double orderPrice, int orderStatus,
            int contentId, ResponseListener<UniversalResponse> responseListener) {
        try {
            JSONObject json = new JSONObject();
            json.put("smartcardid", smartcardid);
            json.put("deviceId", deviceId);
            json.put("orderType", orderType);
            json.put("orderPrice", orderPrice);
            json.put("orderStatus", orderStatus);
            json.put("contentId", contentId);

            JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL + "/REST/order/addOrder", json,
                    new UniversalParser(), responseListener);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取转换Url
     * 
     */
    public static void getUrl(String url, ResponseListener<PlayUrlBean> responseListener) {
        JSONObject json = new JSONObject();
        JsonRequestManager.getInstance().jsonRequest(url, json, new PlayUrlParser(), responseListener);

    }

    /**
     * 支付异步通知接口(不用，此接口是提供给支付平台的)
     * 
     */
    public static void getpayCallback(String orderId, String paySeq, String payState, String payDesc, String payType,
            ResponseListener<EmptyResponse> responseListener) {
        JSONObject json = new JSONObject();
        try {
            json.put("orderId", orderId);
            json.put("paySeq", paySeq);
            json.put("payState", payState);
            json.put("payDesc", payDesc);
            json.put("payType", payType);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonRequestManager.getInstance().jsonRequest(Constant.BASE_URL, json, new EmptyParser(), responseListener);

    }

}
